package mvpfoot.utils;

import android.content.Context;
import android.content.res.XmlResourceParser;

import com.example.click.mvpfoot.R;

import org.w3c.dom.Document;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.util.ArrayList;

import mvpfoot.MyApplication;
import mvpfoot.entity.UrlData;

/**
 * Created by Chen qin lang on 2017/1/6.
 */
public class MockService {
    private static ArrayList<UrlData> urlList ;

    /**
     * 从xml中加载运行是的数据
     * @param
     */
    private static void getXmlInfo() {
        urlList = new ArrayList();
        XmlResourceParser parser = MyApplication.application.getResources().getXml(R.xml.url);
        int evnetCode;
        try {
            evnetCode = parser.getEventType();
            while (evnetCode != XmlPullParser.END_DOCUMENT) {
                switch (evnetCode) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String name = parser.getName();
                        if ("Node".equals(parser.getName())) {
                            UrlData urlData = new UrlData();
                            urlData.setKey(parser.getAttributeValue(null, "Key"));
                            urlData.setUsername(parser.getAttributeValue(null, "Username"));
                            urlData.setPassword(parser.getAttributeValue(null, "Password"));
                            urlData.setExpires(parser.getAttributeValue(null, "Expires"));
                            urlList.add(urlData);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                }
                evnetCode=parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static UrlData findUrl(String findKey){
        if (urlList==null||urlList.size()==0){
            getXmlInfo();
        }
        for (UrlData urlData:urlList){
            if (findKey.equals(urlData.getKey())){
                return urlData;
            }
        }
        return null;
    }
}
